package com.jubotech.business.web.controller.user;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jubotech.business.web.domain.CustomerInfo;
import com.jubotech.business.web.dto.RegisterDTO;
import com.jubotech.business.web.service.CustomerService;
import com.jubotech.framework.common.ResultInfo;

@Controller
public class RegisterController {
 
	
	@Autowired 
	private CustomerService customerService;
	
	@GetMapping("/user/register")
	public ModelAndView login() {
		return new ModelAndView("/user/register");
	}
 
	@PostMapping("/user/register")
	@ResponseBody
	public ResultInfo login(HttpServletRequest request, RegisterDTO dto) {
		if (StringUtils.isBlank(dto.getUserName())) {
			return ResultInfo.fail("账号不能为空");
		}
		if (StringUtils.isBlank(dto.getPassword())) {
			return ResultInfo.fail("密码不能为空");
		}
		if (StringUtils.isBlank(dto.getTeamName())) {
			return ResultInfo.fail("团队名不能为空");
		}
		 
		if (StringUtils.isBlank(dto.getPhone())) {
			return ResultInfo.fail("手机号不能为空");
		}
 
		CustomerInfo user = customerService.getCustomerInfoByAccount(dto.getUserName());
		if (null != user) {
			return ResultInfo.fail("账号已存在，请换一个");
		}
		
		CustomerInfo customer = new CustomerInfo();
		customer.setAccount(dto.getUserName());
		customer.setPassword(dto.getPassword());
		customer.setType(0);
		customer.setContact(dto.getTeamName());
		customer.setPhone(dto.getPhone());
		customer.setState(1);//默认正常
		customer.setDevice_num(100);//设备个数，默认100个
		customer.setCreateTime(new Date());
		  
		return customerService.insert(customer);
	}
 

}
